package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.SysDeptPropertyEntity;
import com.ly.heart.mapper.SysDeptPropertyMapper;
import com.ly.heart.service.ISysDeptPropertyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 部门属性表 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-09-21
 */
@Service
public class SysDeptPropertyServiceImpl extends ServiceImpl<SysDeptPropertyMapper, SysDeptPropertyEntity> implements ISysDeptPropertyService {

    @Override
    public AjaxResult queryList (SysDeptPropertyEntity sysDeptPropertyEntity) {
        Page<SysDeptPropertyEntity> page = this.page ( new Page<> ( sysDeptPropertyEntity.getPage (), sysDeptPropertyEntity.getLimit () ), new LambdaQueryWrapper<SysDeptPropertyEntity> ().eq ( SysDeptPropertyEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        return AjaxResult.success ( page );
    }

    @Override
    public AjaxResult saveSingle (SysDeptPropertyEntity sysDeptPropertyEntity) {
        sysDeptPropertyEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        sysDeptPropertyEntity.setCode ( "BMSX".concat ( String.format ( "%06d", count ) ) );
        sysDeptPropertyEntity.setUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        sysDeptPropertyEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( sysDeptPropertyEntity );
        if ( ! save ) {
            throw new CustomException ( "新增部门属性失败！" );
        }
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult updateSingle (SysDeptPropertyEntity sysDeptPropertyEntity) {
        sysDeptPropertyEntity.setChangeId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        sysDeptPropertyEntity.setChangeTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( sysDeptPropertyEntity );
        if ( ! b ) {
            throw new CustomException ( "修改部门属性失败！" );
        }
        return AjaxResult.success ();
    }

    @Override
    public AjaxResult deleteBatch (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<SysDeptPropertyEntity> ().set ( SysDeptPropertyEntity :: getIsUsed, ConstUtil.IS_USED_NO ).in ( SysDeptPropertyEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除部门属性失败！" );
        }
        return AjaxResult.success ();
    }
}
